package com.ease.concurrent.thread.communication;

/**
 * 自旋的方式实现两个线程交替打印
 *
 * @author: chenhs
 * @date: Created in 11:33 2019/12/31
 **/
public class Demo3_CAS {

    static volatile int t = 1;
    static volatile int i = 0;

    static int limit = 10;

    public static void main(String[] args) {

        new Thread(() -> {
            while (i < limit) {
                while (t != 1){
                }
                System.out.println("t1:" + (++i));
                t = 2;
            }
        }).start();

        new Thread(() -> {
            while (i < limit) {
                while (t != 2){
                }
                System.out.println(" t2:" + (++i));
                t = 1;
            }
        }).start();

    }

}
